package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.basic.util.CodeGenerateUtils;
import cn.itsource.pethome.basic.util.DistanceUtil;
import cn.itsource.pethome.basic.util.Point;
import cn.itsource.pethome.basic.util.SystemContext;
import cn.itsource.pethome.order.domain.PetAcquisitionOrder;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.PetDetail;
import cn.itsource.pethome.pet.domain.SearchMasterMessage;
import cn.itsource.pethome.order.mapper.PetAcquisitionOrderMapper;
import cn.itsource.pethome.pet.mapper.PetDetailMapper;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pethome.pet.service.ISearchMasterMessageService;
import cn.itsource.pethome.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 发布寻主信息service
 */
@Service
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage> implements ISearchMasterMessageService {
    //商店
    @Autowired
    private ShopMapper shopMapper;
    //寻主信息
    @Autowired
    private SearchMasterMessageMapper searchMasterMessageMapper;
    //宠物
    @Autowired
    private PetMapper petMapper;
    //宠物详情
    @Autowired
    private PetDetailMapper petDetailMapper;
    //宠物详情
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;


    /**
     * 发布寻主信息
     *
     * @param searchMasterMessage
     */
    @Override
    @Transactional
    public void SearchMasterMessage(SearchMasterMessage searchMasterMessage) {
        //获取根据发布地址随机分配附近的商家,根据地址返回经纬度
        Point point = DistanceUtil.getPoint(searchMasterMessage.getAddress());
        Shop shop = DistanceUtil.getNearestShop(point,shopMapper.loadAll());
        //将最近的商店设置进去
        searchMasterMessage.setShop(shop);
        //保存寻主信息
        searchMasterMessageMapper.save(searchMasterMessage);

    }

    /**
     * 处理信息
     *
     * @param pet
     */
    @Override
    @Transactional
    public void handlePendingMessage(Pet pet) {

        //获取寻主信息id
        Long messageId = pet.getSearchMasterMessage().getId();
        //根据寻主信息id查询寻主对象
        SearchMasterMessage searchMasterMessage = searchMasterMessageMapper.loadById(messageId);//需要写sql语句
        //标记寻主信息已被处理
        searchMasterMessage.setState(1);
        //修改数据
        searchMasterMessageMapper.update(searchMasterMessage);//需要写sql语句

        //设置寻主信息
        pet.setSearchMasterMessage(searchMasterMessage);
        //保存宠物
        petMapper.save(pet);//需要写sql语句
        //保存宠物详情
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet(pet);
        petDetailMapper.save(petDetail);//需要写sql语句

        //创建收购订单
        PetAcquisitionOrder petAcquisitionOrder = createOrder(searchMasterMessage,pet);
        //更新保存
        petAcquisitionOrderMapper.save(petAcquisitionOrder);
    }

    /**
     * 创建订单方法
     * @param searchMasterMessage
     * @return
     */
    private PetAcquisitionOrder createOrder(SearchMasterMessage searchMasterMessage,Pet pet) {
        //创建收购订单对象
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        //摘要
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        //0 待支付(余额支付) 1 待报账(垫付) 2 待打款(银行转账)  3 完成
        //判断收购价格是否为0
        if (searchMasterMessage.getPrice().equals(0)){
            //如果为0 设置订单为完成状态
            order.setState(3);
        }else {
            //如果不为0 设置订单为垫付状态。默认使用垫付方式给用户结账
            order.setState(1);
        }
        //收购地址
        order.setAddress(searchMasterMessage.getAddress());
        //宠物
        order.setPet(pet);
        //信息发布者
        order.setUser(searchMasterMessage.getUser());
        //支付类型  0垫付 1余额 2银行转账
        //设置默认支付方式为垫付
        order.setPaytype(0);
        //收购订单对应的店铺
        order.setShop(searchMasterMessage.getShop());
        //员工
        order.setEmployee(SystemContext.getSystemLoginUser());
        //收购价格
        order.setPrice(new BigDecimal(searchMasterMessage.getPrice()));
        //订单编号
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(searchMasterMessage.getUser().getId()));
        //返回订单
        return order;
    }

}
